﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;

namespace PhoneApp1.Model
{
    public class AllDataContext : DataContext
    {
        //Pass the connection string to the base class
        public AllDataContext(string connectionString)
            : base(connectionString)
        { }

        //Specify Table for each items

        //Income table
        public Table<Income> IncomeItems;
        //Expense table
        public Table<Expense> ExpenseItems;
    }

    [Table]
    public class Income : INotifyPropertyChanged, INotifyPropertyChanging
    {
        //define ID: private field, public properties, database column
        //ID
        private string _incomeID;
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public string IncomeID
        {
            get { return _incomeID; }
            set
            {
                if (_incomeID != value)
                {
                    NotifyPropertyChanging("IncomeID");
                    _incomeID = value;
                    NotifyPropertyChanged("IncomeID");
                }
            }
        }

        //Name
        private string _incomeName;
        [Column(CanBeNull = false)]
        public string IncomeName
        {
            get { return _incomeName; }
            set
            {
                if (_incomeName != value)
                {
                    NotifyPropertyChanging("IncomeName");
                    _incomeName = value;
                    NotifyPropertyChanged("IncomeName");
                }
            }
        }

        //Value
        private Int16 _incomeValue;
        [Column(CanBeNull = false)]
        public Int16 IncomeValue
        {
            get { return _incomeValue; }
            set
            {
                if (_incomeValue != value)
                {
                    NotifyPropertyChanging("IncomeValue");
                    _incomeValue = value;
                    NotifyPropertyChanged("Incomevalue");
                }
            }
        }

        //Time you create imcome
        private DateTime _incomeTime;
        [Column(CanBeNull = false)]
        public DateTime IncomeTime
        {
            get { return _incomeTime; }
            set
            {
                if (_incomeTime != value)
                {
                    NotifyPropertyChanging("IncomeTime");
                    _incomeTime = value;
                    NotifyPropertyChanged("IncomeTime");
                }
            }
        }

        // Income's Category
        private int _category;
        [Column(CanBeNull = false)]
        public int Category
        {
            get { return _category; }
            set
            {
                if (_category != value)
                {
                    NotifyPropertyChanging("Category");
                    _category = value;
                    NotifyPropertyChanged("Category");
                }
            }
        }

        [Column(IsVersion = true)]
        private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion

    }
    [Table]
    public class Expense : INotifyPropertyChanged, INotifyPropertyChanging
    {
        //define ID: private field, public properties, database column
        //ID
        private string _expenseID;
        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public string expenseID
        {
            get { return _expenseID; }
            set
            {
                if (_expenseID != value)
                {
                    NotifyPropertyChanging("ExpenseID");
                    _expenseID = value;
                    NotifyPropertyChanged("ExpenseID");
                }
            }
        }

        //Name
        private string _expenseName;
        [Column(CanBeNull = false)]
        public string ExpenseName
        {
            get { return _expenseName; }
            set
            {
                if (_expenseName != value)
                {
                    NotifyPropertyChanging("expenseName");
                    _expenseName = value;
                    NotifyPropertyChanged("expenseName");
                }
            }
        }

        //Value
        private Int16 _expenseValue;
        [Column(CanBeNull = false)]
        public Int16 ExpenseValue
        {
            get { return _expenseValue; }
            set
            {
                if (_expenseValue != value)
                {
                    NotifyPropertyChanging("ExpenseValue");
                    _expenseValue = value;
                    NotifyPropertyChanged("Expensevalue");
                }
            }
        }

        //Time you create expense
        private DateTime _expenseTime;
        [Column(CanBeNull = false)]
        public DateTime ExpenseTime
        {
            get { return _expenseTime; }
            set
            {
                if (_expenseTime != value)
                {
                    NotifyPropertyChanging("ExpenseTime");
                    _expenseTime = value;
                    NotifyPropertyChanged("ExpenseTime");
                }
            }
        }

        // expense's Category
        private int _category;
        [Column(CanBeNull = false)]
        public int Category
        {
            get { return _category; }
            set
            {
                if (_category != value)
                {
                    NotifyPropertyChanging("Category");
                    _category = value;
                    NotifyPropertyChanged("Category");
                }
            }
        }

        [Column(IsVersion = true)]
        private Binary _version;

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify that a property changed
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region INotifyPropertyChanging Members

        public event PropertyChangingEventHandler PropertyChanging;

        // Used to notify that a property is about to change
        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        #endregion
    }
}
